package com.lottery.obtainData.AchieveDataImpl;

import com.lottery.dto.Changlong;
import com.lottery.dto.ObtainDataDto;
import com.lottery.dto.WinProperty;
import com.lottery.entity.Lottery;
import com.lottery.initData.InitData;
import com.lottery.obtainData.AchieveData;
import com.lottery.obtainData.SscYesWinningFactory.BJSCWinResult;
import com.lottery.utils.AchieveDataUtil;
import com.lottery.utils.ChanglongComparator;
import com.lottery.utils.DateUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class BJSCAchieveDataImpl extends AchieveData {
    // 中奖信息标志key
    @Value("${BJSCFLG}")
    private String FLG;
    // 当前开奖的期号
    @Value("${BJSCEXPECT}")
    private String EXPECT;
    // 当前开奖的号码
    @Value("${BJSCOPENCODE}")
    private String OPENCODE;
    // 当前开奖的时间
    @Value("${BJSCOPENDATE}")
    private String OPENDATE;
    // 下一次开奖的时间
    @Value("${BJSCNEXTTIME}")
    private String NEXTTIME;
    // 下一次开奖的期号
    @Value("${BJSCNEXTEXPECT}")
    private String NEXTEXPECT;
    @Value("${BJSCLOTTERYINTERFACE}")
    private String LOTTERYINTERFACE;
    // 开始下注
    @Value("${OPENNUM}")
    private String OPENNUM;
    // 开奖信息前缀
    @Value("${BEFOR}")
    private String BEFOR;
    // 开奖信息后缀
    @Value("${AFTER}")
    private String AFTER;

    @Override
    protected void insertPromptNotice(WinProperty ssc) {
        String expect = ssc.getExpect();
        String opencode = ssc.getOpencode();
        String rule = ssc.getRule();
        msgNoticeService.insertPromptNotice(
                BEFOR + expect + OPENNUM + opencode.replace(",", " ") + " " + rule + AFTER, lotteyType);
    }

    @Override
    protected String StartAction() throws Exception {
        ObtainDataDto obtainData = AchieveDataUtil.getObtainBjscInfo(LOTTERYINTERFACE);
        /************************* ** 补数据 ***************************/
        //checkOut1();
        // 最近开奖的信息
        /******************************************************/
        if (obtainData != null) {
            // 当前开奖期号,当前开奖号码,当前开奖时间,
            try {
                jedisClient.set(EXPECT, obtainData.getExpect());
                jedisClient.set(OPENCODE, obtainData.getOpencode());
            } catch (Exception e) {
                e.printStackTrace();
                logger.error("redis connect error", e);
            }
        }
        // 下一期开奖时间,下一期开奖期号
        jedisClient.set(NEXTEXPECT, obtainData.getNextExpect());
        jedisClient.set(NEXTTIME, obtainData.getNextOpenTime());
        return obtainData.getNextOpenTime();
    }

    @Override
    protected boolean judgeWinBet(WinProperty property) {
        boolean flag = false;
        ObtainDataDto obtain = AchieveDataUtil.getObtainInfo(LOTTERYINTERFACE);
        if (obtain == null) {
            logger.debug("get null.........");
            flag = true;
        }else{
            String opencode = obtain.getOpencode();
            // 将开奖号码做处理,转换成10位号码:"06,03,09,02,01,04,05,07,08,10"
            if (opencode == null) {
                logger.debug("bjsc opencode get null.........");
                flag = true;
            }
            String expect = obtain.getExpect();

            // 从redis中查询上一期的开奖期号
            String expectRedis = jedisClient.get(EXPECT);

            // 如果本次获取的开奖信息和上次相同,就结束本次循环
            if (expect.equals(expectRedis)) {
                flag = true;
            }

            // 查看数据库中是否有该期号的数据,如果有,就结束本次循环
            Lottery selectLotteryByExpect = lotteryDao.selectLotteryDetail(expect, lotteyType);
            if (selectLotteryByExpect != null) {
                flag = true;
            }

            //将数据转封装
            property.setExpect(expect);
            property.setOpencode(opencode);
            property.setNextTime(obtain.getNextOpenTime());
            property.setNextExpect(obtain.getNextExpect());
            property.setOpenTime(obtain.getOpentime());
        }
        return flag;
    }

    @Override
    protected boolean getWinInfomation(WinProperty property) {
        boolean flag = false;
        // 如果本次获取的开奖信息和上次不同,就将本次的开奖信息存入数据库并跟新redis中的内容
        String opencode = property.getOpencode();
        String expect = property.getExpect();

        String nextExpect = property.getNextExpect();
        String nextOpentime = property.getNextTime();

        // 当前开奖期号,当前开奖号码,当前开奖时间,
        jedisClient.set(EXPECT, expect);
        jedisClient.set(OPENCODE, opencode);
        jedisClient.set(OPENDATE, property.getOpenTime());

        // 下一期开奖时间,下一期开奖期号
        jedisClient.set(NEXTEXPECT, nextExpect);
        jedisClient.set(NEXTTIME, nextOpentime);
       //logger.info("get next Expect and NextOpenTime:{}",nextExpect + " + " + nextOpentime);
        Date openTime = null;
        try {
            openTime = DateUtil.getDateFromString(property.getOpenTime(), "yyyy-MM-dd HH:mm");
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //初始化开奖结果集
        //得到开奖结果
        BJSCWinResult bjsc = new BJSCWinResult();
        bjsc.init(opencode);

        String result = getResultInfo();
        // 插入数据库
        Boolean insertLottery = insertLottery(expect,Integer.parseInt(expect), opencode,
                openTime,result);
        if (!insertLottery) {
            logger.error("插入数据库出错");
            flag = true;
        }
        dealWithChanglong();
        return flag;
    }

    private void dealWithChanglong(){
        InitData.BJSC_SORT_CHANGLONG.clear();
        Map<Integer, List<String>> bjscWin = InitData.BJSC_WINRESULT_MAP;
        List<String> strings = new ArrayList<>();
        //排序要用到的玩法(大小单双,猜龙虎,特码,庄闲)
        Integer[] ss = new Integer[]{10,12,13,15};
        for(Integer z: ss){
            bjscWin.get(z).forEach(k->{
                strings.add(k);
            });
        }

        List<Changlong> list = InitData.BJSC_STORE_CHANGLONG;
        list.forEach((k)->{
            if(strings.contains(k.getBetRule())){
                k.setContinueNumber(k.getContinueNumber()+1);
            }else{
                k.setContinueNumber(0);
            }
            InitData.BJSC_SORT_CHANGLONG.add(k);
        });
        InitData.BJSC_SORT_CHANGLONG.sort(new ChanglongComparator());

    }

    private String getResultInfo() {
        Map<Integer, List<String>> bjsc = InitData.BJSC_WINRESULT_MAP;
        String result = "";
        //获取冠亚中奖结果集(前半部分展示)
        String s = bjsc.get(16).get(0).substring(2);
        result += s + ",";
        List<String> strings = bjsc.get(15);
        for (String ss :strings) {
            if(ss.length()>1){
                result +=ss.substring(2)+",";
            }
        }
        result =result.substring(0,result.length()-1)+"&&";
        //获取龙虎中奖结果集(后半部分展示)
        List<String> longhu = bjsc.get(12);
        for(String aa :longhu){
            result+=aa.substring(2)+",";
        }
        result = result.substring(0,result.length()-1);
        return result;
    }

    @Override
    protected void robotBet(WinProperty property) {

    }

    @Override
    protected void selectWinningResultsService(WinProperty property) {

    }

    @Override
    protected void YesWinningFactory(WinProperty bjsc) {
        String opencode = bjsc.getOpencode();
        String expect = bjsc.getExpect();
        lotteryDrawService.bjscYesWinningFactory(opencode, expect, lotteyType);

    }
}
